﻿#ifndef VIDEOHANDLER_H
#define VIDEOHANDLER_H
#include <QXmlDefaultHandler>
#include <QDebug>

class VideoHandler : public QXmlDefaultHandler
{
public:
    enum{
        _unknow,
        _in_video,
        _in_actor,
        _in_summary,
    };
    VideoHandler():m_nState(_unknow) {}
    bool characters(const QString &ch) {//在进入完startElement函数后，进入此函数 为什么？  整理当前元素的子值。
        switch (m_nState) {
        case _in_summary:
        case _in_actor:
            m_content += ch;
           qDebug() << m_content << ch;
            break;
        default:
            break;
        }
        return true;
    }
    bool endElement(const QString &, const QString &, const QString &)
    {
        switch (m_nState) {
        case _in_actor:
//            m_nState = _in_video;
//            m_actors = m_content;
//            m_content.clear();
//            break;
        case _in_summary:
            m_nState = _in_video;
            m_summary = m_content;
            m_content.clear();
            break;
        case _in_video:
            m_nState = _unknow;
        default:
            break;
        }
        qDebug() << m_nState;
        return true;
    }

    bool error(const QXmlParseException &exception) {
        qDebug() << QString("error - %1 %2 %3").arg(exception.message()).arg(exception.lineNumber()).arg(exception.columnNumber());
        resetState();
        return false;
    }
/*
 * bool QXmlContentHandler::startElement(const QString & namespaceURI, const QString & localName, const QString & qName, const QXmlAttributes & atts)
*/

    //当进入第一个元素标记时，此时的qname[qualified name] 为tag，localName为tag，value为对应的值
        /* 需要由你控制如何读取，即怎么读取它的根，它的子分支，它的孙子分支。而QDOmDocument则直接将这些事都做好，为你准备好一棵树并提供丰富的API供你调用。
    */
        /*
         * 调试第一次执行：namespaceURI为空，localName为‘video’，qName为‘video’，atts为‘属性目标：包含三个qname（限定符名称）’
         * 调试执行第一次，就将第一行的数据给获取到了？
    */
    bool startElement(const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &atts)
    {
//        qDebug() << this->errorString();
        switch (m_nState) {
        case _unknow:
            if (qName == QString("video")) {
                m_tags = atts.value("tag");
                m_pageUrl = atts.value("page");
                m_videoName = atts.value("name");
                m_nState = _in_video;
            }//此时会得到一些数据
            break;
        case _in_video:
            if (qName == "actor") m_nState = _in_actor;
            else if (qName == "summary") m_nState = _in_summary;
            break;
        default:
            break;
        }
        return true;
    }

    bool warning(const QXmlParseException &exception) {
        qWarning() << "warning -" << exception.message();
        return true;
    }

    QString m_videoName;
    QString m_pageUrl;
    QString m_actors;
    QString m_summary;
    QString m_tags;
protected:
    void resetState() {
        m_content.clear();
        m_nState  = _unknow;
    }

    QString m_content;
    int m_nState;
};

#endif // VIDEOHANDLER_H
